﻿using LiteDB;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Mirai.Net.Api.ApplicationModel;
using Mirai.Net.Api.Contract;
using Mirai.Net.Api.Model;
using Mirai.Net.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mirai.Net.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class RobotListenController : ControllerBase
    {
        private readonly MiraiClient miraiClient;
        private readonly MiraiDbContext miraiDbContext;

        public RobotListenController(MiraiClient miraiClient, MiraiDbContext miraiDbContext)
        {
            this.miraiClient = miraiClient;
            this.miraiDbContext = miraiDbContext;
        }

        [HttpPost("configure/{event}/{validateType}")]
        public async Task<object> ConfigureAsync([FromRoute] RobotEvent @event, [FromRoute] ValidateType validateType, [FromBody] RobotListenerConfigureModel url)
        {
            var robot = await miraiDbContext.Robots.Include(x => x.Listeners).FirstOrDefaultAsync();
            var listenerConfigure = robot.Listeners.FirstOrDefault(x => x.RobotEvent == @event);
            listenerConfigure.ValidateType = validateType;
            listenerConfigure.Url = url.Url;
            MiraiData.Robot = robot;
            await miraiDbContext.SaveChangesAsync();
            return new
            {
                status = 0
            };
        }

        [HttpGet("send/{qq}/{message}")]
        public async Task SendFriendMessageAsync([FromRoute] string qq, [FromRoute] string message)
        {
            await miraiClient.ConnectAsync();
            var robot = await miraiDbContext.Robots.AsNoTracking().FirstOrDefaultAsync();
            await miraiClient.VerifyAsync(robot.QQ);
            await miraiClient.SendFriendMessageAsync(qq, message);
        }


        [HttpGet("list")]
        public async Task<object> GetListAsync()
        {
            var robot = await miraiDbContext.Robots.Include(x => x.Listeners).FirstOrDefaultAsync();
            if (robot.Listeners == null)
            {
                robot.Listeners = new List<RobotEventListener>();
            }

            var robotEvents = Enum.GetValues(typeof(RobotEvent)).Cast<RobotEvent>();
            foreach (var item in robotEvents)
            {
                if (!robot.Listeners.Any(x => x.RobotEvent == item))
                {
                    robot.Listeners.Add(new RobotEventListener()
                    {
                        RobotEvent = item
                    });
                }
            }
            await miraiDbContext.SaveChangesAsync();
            return new
            {
                items = robot.Listeners.Select(x => new
                {
                    eventType = x.RobotEvent,
                    eventName = GetEventName(x.RobotEvent),
                    configure = GetConfigureType(x.ValidateType)
                }).ToList(),
                total = robot.Listeners.Count
            };
        }

        private string GetConfigureType(ValidateType validateType)
        {
            switch (validateType)
            {
                case ValidateType.None:
                    return "没有配置";
                case ValidateType.NoValidate:
                    return "不验证，直接通过";
                case ValidateType.RemoteValidate:
                    return "远程验证";
            }
            return validateType.ToString();
        }

        private string GetEventName(RobotEvent key)
        {
            switch (key)
            {
                case RobotEvent.BotJoinGroupEvent:
                    return "进群";
                case RobotEvent.FriendMessage:
                    return "收到好友消息";
                case RobotEvent.GroupMessage:
                    return "收到群消息";
                case RobotEvent.MemberJoinRequestEvent:
                    return "收到加群请求";
                case RobotEvent.NewFriendRequestEvent:
                    return "好友申请";
                case RobotEvent.MemberLeaveEventKick:
                    return "群员被踢";
                case RobotEvent.MemberJoinEvent:
                    return "新人已入群";
            }
            return key.ToString();
        }
    }
}
